package com.itheima.controller;



import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.Result;
import com.itheima.dto.SetmealDto;
import com.itheima.pojo.Category;
import com.itheima.pojo.Dish;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealDish;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private DishService dishService;

//请求 URL: http://localhost:8080/setmeal/page?page=1&pageSize=10
//请求方法: GET
    @GetMapping("/page")
    public Result page(int page,int pageSize,String name){

        IPage<Setmeal> pages = new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> lqw =new LambdaQueryWrapper<>();
        lqw.like(StringUtils.hasLength(name),Setmeal::getName,name).orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pages,lqw);
        List<Setmeal> records = pages.getRecords();

        ArrayList<Setmeal> setmealDtos = new ArrayList<>();

        for (Setmeal setmeal : records) {
            SetmealDto setmealDto = new SetmealDto();
            Long categoryId = setmeal.getCategoryId();
            Category category = categoryService.getById(categoryId);
            BeanUtils.copyProperties(setmeal,setmealDto);
            setmealDto.setCategoryName(category.getName());
            setmealDtos.add(setmealDto);
        }
        pages.setRecords(setmealDtos);
        return Result.success(pages);
    }

//    Request URL: http://localhost:8081/setmeal
//    Request Method: POST
    @PostMapping
    public Result saveSingle(@RequestBody SetmealDto setmealDto){
        setmealService.add(setmealDto);

        return Result.success("添加成功");

    }

    //请求 URL: http://localhost:8080/setmeal/1456156558831390721
    //请求方法: GET
    //1.数据回显

    @GetMapping("{id}")
    public Result goBack(@PathVariable Long id){

        //通过套餐id获得套餐数据
        Setmeal setmeal = setmealService.getById(id);

        //获取套餐下的菜品
        LambdaQueryWrapper<SetmealDish> lqw =new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(lqw);

        //创建一个setmealDto的对象,将setmeal赋值给SetmealDto
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);

        //将菜品赋值给setmealDto
        setmealDto.setSetmealDishes(list);

        return Result.success(setmealDto);
    }

//    请求 URL: http://localhost:8080/setmeal?ids=1456156558831390721
//    请求方法: DELETE

    @DeleteMapping
    public Result deletes(@RequestParam List<Long> ids){

        Result deletes = setmealService.deletes(ids);
        return deletes;
    }

    @PutMapping
    public Result updateSingle(@RequestBody SetmealDto setmealDto){

        setmealService.updates(setmealDto);

        return Result.success("保存成功");
    }

    //    请求 URL: http://localhost:8080/dish/status/0?ids=1413384757047271425
//    请求方法: POST
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable int status, @RequestParam List<Long> ids) {

//        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(Setmeal::getStatus,status).in(Setmeal::getId,ids);
//        int count = setmealService.count(lqw);
//
//        if(count>0)
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.in(Setmeal::getId, ids);
        setmealService.update(setmeal,lqw);

        return Result.success("修改成功");

    }
    //请求 URL: http://localhost:8080/setmeal/list?categoryId=1413386191767674881&status=1
//    请求方法: GET
    @GetMapping("/list")
    public Result listAdd(Long categoryId,int status ){
        ArrayList<SetmealDto> setmealDtos = new ArrayList<SetmealDto>();
        LambdaQueryWrapper<Setmeal> lqw =new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getCategoryId,categoryId).eq(Setmeal::getStatus,status);
        List<Setmeal> list = setmealService.list(lqw);
        for (Setmeal setmeal : list) {
            SetmealDto setmealDto = new SetmealDto();
            Long setmealId = setmeal.getId();
            LambdaQueryWrapper<SetmealDish> qw =new LambdaQueryWrapper<>();
            qw.eq(SetmealDish::getSetmealId,setmealId);
            List<SetmealDish> list1 = setmealDishService.list(qw);
            BeanUtils.copyProperties(setmeal,setmealDto);
            setmealDto.setSetmealDishes(list1);
            setmealDtos.add(setmealDto);
        }
        return Result.success(setmealDtos);
    }

//    请求 URL: http://localhost:8080/setmeal/dish/1456179679705763842
//    请求方法: GET
    @GetMapping("/dish/{setmealId}")
    public Result goBackSetmeal(@PathVariable Long setmealId){

        List<Dish> listDish =new ArrayList<>();
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,setmealId);
        List<SetmealDish> list = setmealDishService.list(lqw);

        for (SetmealDish setmealDish : list) {
            Long dishId = setmealDish.getDishId();
            Dish dish = dishService.getById(dishId);
            listDish.add(dish);
        }
        return Result.success(listDish);



    }
}
